public class QueueMaker extends Pacx {

    def indexDir;
    
    QueueMaker() {
        super()
        log = new File(config.doc4.queue.logfile)
        indexDir = config.doc4.queue.indexdir
        queries = config.doc4.queue.queries
    }

    public static void main(String[] args) {
        def qm = new QueueMaker()
        qm.run()
        qm.sql.close()
    }
    
    public void run() {
        def allresult = 1
        for (distrib in config.doc4.distributions.list) {
            def result = fetchPackageIndex(distrib)
            if (result == 0) {
                    //TODO:remove all entries from the snapshot table
                    
                    loadDistributionIndexSnapshot(distrib)
                }
        
            allresult = allresult && !result;
        }
        
        if (!allresult)
           return;
        
        //makeQueue("download", "queued")
        makeQueue("extraction", "queued")
        makeQueue("tocsv", "queued")
        
        //for (distrib in config.doc4.distributions.list)
        //    makeRsyncQueue(distrib)
    }
    
    def void loadDistributionIndexSnapshot(distrib) {
        log "Loading distribution index snapshot for ${distrib} with package index folder: $indexDir ..."
    
        try {  
            sql.execute(queries.loadsnapshot, [indexDir+"/"+distrib+".idx"])
        } catch (Exception e) {
            println e.getMessage()
            log("*** Error: "+e.getMessage())
        }
        log("Loading ended")
    }
    
    
    def makeQueue(String action, String state) {
        log("Creating $action queue for all loaded distribs")    
        sql.execute(queries.createqueue, [action, state])
    }
    
    
    def void makeRsyncQueue(String distrib) {
        log "Creating rsync queue for ${distrib}..."
        def done = new ArrayList()
        def rsyncExclude = new File("$indexDir/${distrib}.exclude")
        rsyncExclude.setText("")
        
        sql.eachRow(queries.rsyncexclude, [distrib]) {
            row -> 
                def pkg = row.pkg
                def arch = row.architecture_folder+"/"
                def branch = row.branch
                def section = row.section
                def distro = row.distribution
                
             //println "$distro $arch $branch $section $pkg"
             def media = arch.equals("SRPMS/") ? "": "media/"
             arch = distrib.equals("cooker") ? ""  : arch
                     
             //rsyncExclude.append("+ $arch/$media$branch/$section/$pkg\n")
             //if (!state.equals("queued"))
                rsyncExclude.append("$arch$media$branch/$section/$pkg\n")
        }
        
        def archroot = "i586/"
        if (distrib.equals("cooker"))
            archroot=""
            
        rsyncExclude.append("${archroot}media/debug_contrib\n")
        rsyncExclude.append("${archroot}media/debug_main\n")
        rsyncExclude.append("${archroot}media/debug_non-free\n")
        rsyncExclude.append("${archroot}isolinux\n")
        rsyncExclude.append("${archroot}install\n")
        rsyncExclude.append("${archroot}misc\n")
        rsyncExclude.append("${archroot}lang\n")
        rsyncExclude.append("${archroot}doc\n")
        rsyncExclude.append("${archroot}dosutils\n")
        rsyncExclude.append("${archroot}*.txt\n")
        rsyncExclude.append("${archroot}*.html\n")
        rsyncExclude.append("${archroot}COPYING\n")
        rsyncExclude.append("${archroot}VERSION\n")
        rsyncExclude.append("${archroot}*.htm\n")
        rsyncExclude.append("${archroot}autorun.inf\n")
        rsyncExclude.append("${archroot}linuxboot.cfg\n")
        rsyncExclude.append("${archroot}export\n")
        rsyncExclude.append("${archroot}**/media_info/*\n")
        
        
        if (!distrib.equals("cooker"))
            rsyncExclude.append("x86_64\n")
    
    }
    
    
    def int fetchPackageIndex(String distrib) {
    
        def command = "./package-list-fetcher.sh $distrib $indexDir"
        log "Fetching package index for ${distrib}..."
        def process = command.execute()
        process.waitFor()
        def exitValue = process.exitValue()
        log "Exit value: "+exitValue+" Stdout: "+process.err.text+" Stderr: "+process.in.text
    
        return exitValue
    }

}